package com.cp.yn.vm

import androidx.lifecycle.viewModelScope
import com.common.base.base.BaseVM
import com.common.base.http.apiResponse
import com.common.base.http.impl.bean.ChannelStatusInfoBean
import com.common.base.http.impl.bean.LoginResultBean
import com.common.base.http.main.ApiSubscriber
import com.common.base.http.main.execute
import com.common.base.http.requestApi
import com.common.base.impl.http.api.HttpApi
import com.common.base.manager.ConfigManager
import com.common.base.manager.StackManager
import com.common.base.tools.obj.LoadingObj
import com.common.base.tools.utils.JsonUtils
import com.common.base.tools.utils.logger
import com.cp.yn.bean.LoginBean
import com.cp.yn.bean.SmsBean
import com.cp.yn.http.factory.ApiLoginFactory
import com.cp.yn.http.factory.ApiOrderFactory
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 三疯
 * *************************
 *
 * *************************
 */
class LoginVM : BaseVM() {
    private var _flowSendSms = MutableSharedFlow<SmsBean>()
    var flowSendSms: SharedFlow<SmsBean> = _flowSendSms

    private var _flowLogin = MutableSharedFlow<LoginBean>()
    var flowLogin: SharedFlow<LoginBean> = _flowLogin

    /**
     * 示例
     */
    fun apiSendSmsUrl(mobile: String, captchaValue: String, region: String, channel: String) {
        val map: Map<String, Any> = mapOf(
            "mobile" to mobile,
            "captchaValue" to captchaValue,
            "region" to region,
            "channel" to channel,
        )
        //方式一，开启没有生命周期捕获的协程
        CoroutineScope(CoroutineExceptionHandler { _, throwable ->
           //todo 异常阻断流程
        }).launch {
            //todo 正常执行流程
        }

        //方式二，开启没有生命周期的协程
        CoroutineScope(Dispatchers.IO).launch {
            //todo 正常执行流程
        }


        viewModelScope.launch(Dispatchers.IO) {

            //整体请求1
            val response1 = requestApi({
                //串行多个请求，使用多模块api，<ApiLoginFactory> and <ApiOrderFactory>
                //1、加上apiResponse 验证Token 过滤特定的错误
                var bean1 = ApiLoginFactory.login(map).apiResponse()
//                var bean3 = ApiLoginFactory.login(map)

                //重置host
//                val bean2 = ApiOrderFactory.host(ConfigManager.configBean?.baseUrl).apiSendSmsUrl(map) //更换主机URL
                //使用Flow响应流，LiveData会数据倒灌


                //高阶函数,取最后一个作为函数返回值T,自动验证token
                ApiLoginFactory.apiSendSmsUrl(map)


            }, isShowLoading = false)

            logger.info("统一异常:${JsonUtils.toJson(response1)}")

//            _flowSendSms.emit(bean2)

//            //整体请求2
//            val response2 = requestApi({
//                ApiLoginFactory.login(map)
//            }, isShowLoading = false)
//
//            //使用Flow响应流，LiveData会数据倒灌
//            _flowSendSms.emit(response1)
        }

        //java请求
//        ApiLoginFactory.loginJava(map)
//            .execute(object : ApiSubscriber<LoginBean>() {
//                override fun onSuccessHandler(t: LoginBean) {
//                    super.onSuccessHandler(t)
//                }
//
//                override fun onFailHandler(t: LoginBean) {
//                    super.onFailHandler(t)
//                }
//            })

    }

    /**
     * 登录
     */
    fun login(mobile: String, smsCode: String, region: String, channel: String) {
        val map: Map<String, Any> = mapOf(
            "mobile" to mobile,
            "smsCode" to smsCode,
            "region" to region,
            "channel" to channel,
        )
        requestApi({
            ApiLoginFactory.login(map)
            LoginBean()
        }, {
            _flowLogin.emit(it)
        }, isShowLoading = false)


    }
}